import 'package:backend.core/core/domain/entities/app_store/app_store_one_time_purchased_item_transaction.dart';
import 'package:backend.core/core/domain/entities/app_store/app_store_transaction_revocation_info.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_environment.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_one_time_purchased_item_transaction.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_offer_type_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_ownership_type_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_revocation_info_from_api_one.dart';

abstract class CreateAppStoreOneTimePurchasedItemTransactionFromApiOne {
  AppStoreOneTimePurchasedItemTransaction call({
    required AppStoreApiOneTimePurchasedItemTransaction apiOneTimePurchasedItemTransaction,
    required String appStoreUserId,
  });
}

class CreateAppStoreOneTimePurchasedItemTransactionFromApiOneImpl
    implements CreateAppStoreOneTimePurchasedItemTransactionFromApiOne {
  const CreateAppStoreOneTimePurchasedItemTransactionFromApiOneImpl({
    required CreateAppStoreTransactionOfferTypeFromApiOne
        createAppStoreTransactionOfferTypeFromApiOne,
    required CreateAppStoreTransactionOwnershipTypeFromApiOne
        createAppStoreTransactionOwnershipTypeFromApiOne,
    required CreateAppStoreTransactionRevocationInfoFromApiOne
        createAppStoreTransactionRevocationInfoFromApiOne,
  })  : _createAppStoreTransactionOfferTypeFromApiOne =
            createAppStoreTransactionOfferTypeFromApiOne,
        _createAppStoreTransactionOwnershipTypeFromApiOne =
            createAppStoreTransactionOwnershipTypeFromApiOne,
        _createAppStoreTransactionRevocationInfoFromApiOne =
            createAppStoreTransactionRevocationInfoFromApiOne;

  final CreateAppStoreTransactionOfferTypeFromApiOne _createAppStoreTransactionOfferTypeFromApiOne;
  final CreateAppStoreTransactionOwnershipTypeFromApiOne
      _createAppStoreTransactionOwnershipTypeFromApiOne;
  final CreateAppStoreTransactionRevocationInfoFromApiOne
      _createAppStoreTransactionRevocationInfoFromApiOne;

  @override
  AppStoreOneTimePurchasedItemTransaction call({
    required AppStoreApiOneTimePurchasedItemTransaction apiOneTimePurchasedItemTransaction,
    required String appStoreUserId,
  }) {
    final offerType = _createAppStoreTransactionOfferTypeFromApiOne(
      apiOfferType: apiOneTimePurchasedItemTransaction.offerType,
    );

    final ownershipType = _createAppStoreTransactionOwnershipTypeFromApiOne(
      apiOwnershipType: apiOneTimePurchasedItemTransaction.ownershipType,
    );

    AppStoreTransactionRevocationInfo? revocationInfo;

    if (apiOneTimePurchasedItemTransaction.revocationInfo != null) {
      revocationInfo = _createAppStoreTransactionRevocationInfoFromApiOne(
        apiRevocationInfo: apiOneTimePurchasedItemTransaction.revocationInfo!,
      );
    }

    return AppStoreOneTimePurchasedItemTransaction(
      id: apiOneTimePurchasedItemTransaction.id,
      time: apiOneTimePurchasedItemTransaction.time,
      appStoreUserId: appStoreUserId,
      originalTransactionId: apiOneTimePurchasedItemTransaction.originalTransactionId,
      purchaseItemSku: apiOneTimePurchasedItemTransaction.purchaseItemSku,
      offerType: offerType,
      ownershipType: ownershipType,
      test: apiOneTimePurchasedItemTransaction.environment == AppStoreApiEnvironment.sandbox,
      revocationInfo: revocationInfo,
    );
  }
}
